package cn.tedu.tmall.admin.content.service.impl;

import cn.tedu.tmall.admin.content.dao.mapper.CategoryMapper;
import cn.tedu.tmall.admin.content.dao.repository.impl.CategoryRepository;
import cn.tedu.tmall.admin.content.pojo.param.CategoryAddParam;
import cn.tedu.tmall.admin.content.pojo.po.CategoryPO;
import cn.tedu.tmall.admin.content.service.ICategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * 1 通过验证
 * 2 判断类别名称是否存在
 * 3 类别名称不存在 保存
 * 4 存在 抛异常
 */
@Service
@Slf4j
public class CategoryService implements ICategoryService {

//    @Autowired
//    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryRepository categoryRepository;

    @Override
    public void addCategory(CategoryAddParam categoryAddParam) {
        String name = categoryAddParam.getName();
        Integer count=categoryRepository.countCategoryByName(name);
        log.debug("Service 增加类别-通过名称查询类别-入参:{},出参:{}",name,count);
        if (count == null) {
            //类名不存在
            CategoryPO newCategoryPO = new CategoryPO();
            BeanUtils.copyProperties(categoryAddParam, newCategoryPO);
//            categoryMapper.save(newCategoryPO);
            categoryRepository.save(newCategoryPO);
        } else {
            //类别名称存在
            //抛出异常
            throw new RuntimeException("类别名称存在");
        }
    }
}
